<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Session_db {

    var $CI;
    var $session_id;
    var $session_array = array(); //是session实际的值，序列化后存入数组

    public function __construct() {
        $this->CI = & get_instance(); 
        $this->session_id = Session_id(); //开启sessionstart时候可以获取session_id
        $this->CI->load->database();
        //$this->write_Sessiondb("jj", "222"); 
        //  session_regenerate_id();
    }

    public function test() {
        echo "123";    
    }

    public function isResdb($session_id) {//判定根据session_id数据库中是否有记录
        if ($session_id != null) {
            $sessionid = md5($session_id);
            $sql = "select * from sessiondb where sessionid=\"{$sessionid}\"";
            $query = $this->CI->db->query($sql);
            $res = $query->result_array();           
            if (!empty($res)) {
                return $res;
            } else {

                return FALSE;
            }
        }
    }

    public function write_Sessiondb($key, $val) {//如果为空就写入数组,传入数值，相当于给session赋值
        //$val传入参数为数组形式$key表示session名称，值为session保存的数值或者数组
        $session_id = $this->session_id; //获取sessionid
       
        if (!($this->isResdb($session_id))) {//如果为真说明数据库中还没有记录传入sessionid还未md5
            $session_array = $this->session_array;
            $session_array[$key] = $val;
            $session_content = serialize($session_array);
            $session_content = htmlspecialchars($session_content); //待定session应该不用保存中文？
            $time = time();
            $sessionid = md5($session_id);
            $sql = "insert into sessiondb(sessionid,sessionconten,ses_time)values(
       \"{$sessionid}\" , \"{$session_content}\"    ,\"{$time}\"  )";
            $query = $this->CI->db->query($sql);          
        } else {//如果为假说明有记录那么就更新记录
            $this->update_Sessiondb($key, $val);
        }
    }

    public function update_Sessiondb($key, $val) {//更新数据库记录相当于session修改值
        $session_id = $this->session_id; //获取sessionid        
        if ($res = $this->isResdb($session_id)) {//如果为真说明数据库中还没有记录         
         $sesison_content = htmlspecialchars_decode($res[0]["sessionconten"]);
            $sesison_content = unserialize($sesison_content);
            $sesison_content[$key] = $val;
            $session_id = md5($session_id);
            $time = time();
            $sesison_content = serialize($sesison_content);
            $sesison_content = htmlspecialchars($sesison_content);
            $sql = "update sessiondb set sessionconten=\"{$sesison_content}\",ses_time=\"{$time}\"
            where sessionid=\"{$session_id}\"";
            $query = $this->CI->db->query($sql);
            
        } else {

            $this->write_Sessiondb($key, $val);
        }
    }

    public function del_Sessiondb($key) {//传入键值删除特定的session
        $session_id = $this->session_id; //获取sessionid 
        if ($res = $this->isResdb($session_id)) {           
            $sesison_content = htmlspecialchars_decode($res[0]["sessionconten"]);
            $sesison_content = unserialize($sesison_content);
            //删除特定的key值数组元素
            unset($sesison_content[$key]);
            $session_id = md5($session_id);
            $time = time();
            $sesison_content = serialize($sesison_content);
            $sesison_content = htmlspecialchars($sesison_content);
            $sql = "update sessiondb set sessionconten=\"{$sesison_content}\",ses_time=\"{$time}\"
            where sessionid=\"{$session_id}\"
            ";
            $query = $this->CI->db->query($sql);       
        } else {
            
        }
    }

    public function get_Sessiondb($key) {
        $session_id = $this->session_id; //获取sessionid 
        if ($res = $this->isResdb($session_id)) {
            $sesison_content = htmlspecialchars_decode($res[0]["sessionconten"]);
            $sesison_content = unserialize($sesison_content);
            if (isset($sesison_content[$key])) {
                return $sesison_content[$key];
            } else {
                //return $sesison_content;
            }
        }
    }

    public function destory_Sessiondb() {
        $session_id = $this->session_id; //获取sessionid 
        $session_id = md5($session_id);
        $delsql = "delete from sessiondb where sessionid=\"{$session_id}\"";
        $query = $this->CI->db->query($delsql);
        session_regenerate_id();
    }

    public function decis_last_time() {//判断topsession过期
        $_sess_time = 14400; 
        $last_active_time=$this->get_Sessiondb('last_active_time');
        if (!isset($last_active_time)) {
            redirect('login/logout', 'location', 301);
        }        
        if (($last_active_time + $_sess_time) < time()) {
          
            redirect('login/logout', 'location', 301);
        } else {
            $this->write_Sessiondb("last_active_time", time());
        }
    }

}

/* End of file Someclass.php */